//获取应用实例
const app = getApp();
import carMake from '../../utils/carMake.js';
import util from '../../utils/util.js';
Page({
  data: {
    scrollLeft: 0, //左滑
    character: [], //字母表
    carMakeList: [], //主品牌
    subCarMakeList: [], //子品牌
    hidden: true, //字母表提示隐藏
    listCurID: null, //临时存储当前字母
    listCur: null, //最终选择字母
    baseImgUrl: app.globalData.apiImgUrl + 'car/', //车型图片基本路径
    scrollTop: 0, //滚动页面距离顶部距离
    showModal: false, //是否显示车型
    showLevel: 2 //显示级别  用于控制是否显示车型
  },
  brandTap(e) {
    let self = this;
    let mainid = e.currentTarget.dataset.mainid;
    if (self.data.showLevel == 2) {
      self.showModal(mainid);
    } else {
      let carMakeList = carMake;
      for (let i = 0; i < carMakeList.length; i++) {
        if (carMakeList[i].value == mainid) {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面         
          prevPage.setData({
            carBrandModel: carMakeList[i].text,
            carBrand: carMakeList[i].text,
            carBrandId: carMakeList[i].value,
            carModel: '',
            carModelId: ''
          })
          wx.navigateBack();
          return false;
        }
      }
    }
  },
  //显示车型
  showModal(mainid) {
    let self = this;
    let carMakeList = carMake;
    let result = [];
    for (let i = 0; i < carMakeList.length; i++) {
      if (carMakeList[i].value == mainid) {
        if (carMakeList[i].child.length == 0) {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面         
          prevPage.setData({
            carBrandModel: carMakeList[i].text,
            carBrand: carMakeList[i].text,
            carBrandId: carMakeList[i].value,
            carModel: '',
            carModelId: ''
          })
          wx.navigateBack();
          return false;
        } else {
          result = carMakeList[i].child;
        }
      }
    }
    self.setData({
      subCarMakeList: result,
      showModal: true
    })
  },
  //子品牌点击选择
  getSubCarMackTap(e) {
    let self = this;
    let subId = e.currentTarget.dataset.subid;
    let carMakeList = carMake;
    for (let i = 0; i < carMakeList.length; i++) {
      for (let k = 0; k < carMakeList[i].child.length; k++) {
        for (let j = 0; j < carMakeList[i].child[k].child.length; j++) {
          if (carMakeList[i].child[k].child[j].value == subId) {
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2]; //上一个页面           
            self.hideModal();
            prevPage.setData({
              carBrandModel: carMakeList[i].text + '  ' + carMakeList[i].child[k].child[j].text,
              carBrand: carMakeList[i].text,
              carBrandId: carMakeList[i].value,
              carModel: carMakeList[i].child[k].child[j].text,
              carModelId: carMakeList[i].child[k].child[j].value
            })
            wx.navigateBack();
          }
        }
      }
    }
  },
  hideModal(e) {
    this.setData({
      showModal: false
    })
  },
  //获取文字信息
  getCur(e) {
    this.setData({
      hidden: false,
      listCur: e.target.id,
    })
  },

  setCur(e) {
    this.setData({
      hidden: true,
      listCur: this.data.listCur
    })
  },
  //滑动选择Item
  tMove(e) {
    let y = e.touches[0].clientY,
      offsettop = this.data.boxTop,
      self = this;
    let list = self.data.character;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      let num = parseInt((y - offsettop) / 20);
      console.log(num);
      self.setData({
        listCur: list[num]
      })
    };
  },

  //触发全部开始选择
  tStart() {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd() {
    this.setData({
      hidden: true,
      listCurID: this.data.listCur
    })
  },
  //获取车型列表
  getMakeData: function () {
    let self = this;
    let carMakeList = carMake;
    let result = []; //最终结果
    let character = []; //首字母
    for (let i = 0; i < carMakeList.length; i++) {
      var cari = carMakeList[i];
      if (character.indexOf(cari.capital) < 0) {
        result.push({
          characterTitle: cari.capital,
          characterList: [cari]
        });
        character.push(cari.capital);
      } else {
        for (let j = 0; j < result.length; j++) {
          var dj = result[j];
          if (dj.characterTitle == cari.capital) {
            dj.characterList.push(cari);
            break;
          }
        }
      }
    };
    self.setData({
      character: character.sort(),
      carMakeList: result.sort(util.objCompare("characterTitle", true)),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    self.setData(options);
    self.getMakeData();
    // console.log(carMake);
    self.setData({
      scrollTop: app.globalData.CustomBar + 'px'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let self = this;
    wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
      self.setData({
        boxTop: res.top
      })
    }).exec();
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
      self.setData({
        barTop: res.top
      })
    }).exec()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})